
import com.alibaba.fastjson.JSONObject;

/**
 * Created by donar on 17/3/29.
 */
public class JsonUtil {
    public static Object getValueByPath(JSONObject jsonObject,String path){
        try{
            String[] nodes = path.split("\\.");
            JSONObject current = jsonObject;
            for (int i = 0; i < nodes.length-1 ; i++) {
                current=current.getJSONObject(nodes[i]);
            }
            return current.get(nodes[nodes.length-1]);
        }catch (Exception e){
            return null;
        }
    }
    public static JSONObject getJSONObjectByPathWithFuzzy(JSONObject jsonObject,String path){
        try{
            String[] nodes = path.split("\\.");
            JSONObject current = jsonObject;
            for (int i = 0; i < nodes.length ; i++) {
                if(nodes[i].equals("*")){
                    for (String key:current.keySet()){
                        current=current.getJSONObject(key);
                    }
                }
                else current=current.getJSONObject(nodes[i]);
            }
            return current;
        }catch (Exception e){
            return null;
        }
    }
}
